import collections
class Solution(object):
    def countServers(self, grid):
        m = len(grid)
        n = len(grid[0])
        h_dict = collections.defaultdict(int)
        v_dict = collections.defaultdict(int)
        for i in range(m):
            for j in range(n):
                if grid[i][j]:
                    h_dict[i] += 1
                    v_dict[j] += 1
        ans = 0
        for i in range(m):
            for j in range(n):
                if grid[i][j] and (h_dict[i] > 1 or v_dict[j] > 1):
                    ans += 1
        return ans